$(function() {
    // console.log("引入成功");
    // 存放方块里面的数据
    const numArr = new Array(16);
    let randomArr = [];
    arrInit();
    var action = '';
    var maxScore = 0;
    var gameScore;
    $("#flash").click(function() {
        // console.log("按钮点击事件");
        window.location.reload();
    })
    numInit();
    // localStorage.maxScore = 0;
    if (localStorage.maxScore) {
        maxScore = localStorage.maxScore - 0;
    } else {
        maxScore = 0;
        localStorage.maxScore = 0;
    }
    $('#maxScore').html(localStorage.maxScore);
    // testJQuery();
    // ---------------------测试函数---------------------------
    function testJQuery() {
        console.log("函数执行");
        // 尝试获得页面元素
        var maxScore = $('#maxScore');
        console.log(maxScore.eq(0).text());

        // 尝试修改页面元素
        gameScore = 110;
        $('#nowScore').html(gameScore);

        // 尝试修改2048元素
        var item = $('.gameBox .row .item');
        console.log(item);
        for (var i = 0; i < 16; i++) {
            item.eq(i).html(i);
        }
        // // item.eq(numPosition(2, 3)).html(1000);
        // console.log(numArr);
    }


    // ------------------------------2048数字操作-------------------------------------------
    // 将x和y转化为对应的坐标
    function numPosition(x, y) {
        return x * 4 + y;
    }

    // 初始化分数
    function arrInit() {
        for (var i = 0; i < 16; i++) {
            numArr[i] = 0;
        }
    }
    // 移动事件
    function numMove() {

        // console.log(action);
        if (action == "left") {
            for (var i = 0; i < 4; i++) {
                // 数字重合操作
                for (var j = 3; j > 0; j--) {
                    if (numArr[numPosition(i, j)] == numArr[numPosition(i, j - 1)]) {
                        numArr[numPosition(i, j - 1)] += numArr[numPosition(i, j)];
                        numArr[numPosition(i, j)] = 0;
                    }
                }
                // 数字堆叠操作
                for (var j = 3; j > 0; j--) {
                    if (numArr[numPosition(i, j - 1)] == 0) {
                        numArr[numPosition(i, j - 1)] = numArr[numPosition(i, j)];
                        numArr[numPosition(i, j)] = 0;
                    }
                }
            }
        } else if (action == "right") {
            for (var i = 0; i < 4; i++) {
                // 数字重合操作
                for (var j = 1; j < 4; j++) {
                    if (numArr[numPosition(i, j)] == numArr[numPosition(i, j - 1)]) {
                        numArr[numPosition(i, j)] += numArr[numPosition(i, j - 1)];
                        numArr[numPosition(i, j - 1)] = 0;
                    }
                }
                // 数字堆叠操作
                for (var j = 1; j < 4; j++) {
                    if (numArr[numPosition(i, j)] == 0) {
                        numArr[numPosition(i, j)] = numArr[numPosition(i, j - 1)];
                        numArr[numPosition(i, j - 1)] = 0;
                    }
                }
            }
        } else if (action == "up") {
            for (var j = 0; j < 4; j++) {
                // 数字重合操作
                for (var i = 3; i > 0; i--) {
                    if (numArr[numPosition(i, j)] == numArr[numPosition(i - 1, j)]) {
                        numArr[numPosition(i - 1, j)] += numArr[numPosition(i, j)];
                        numArr[numPosition(i, j)] = 0;
                    }
                }
                // 数字堆叠操作
                for (var i = 3; i > 0; i--) {
                    if (numArr[numPosition(i - 1, j)] == 0) {
                        numArr[numPosition(i - 1, j)] = numArr[numPosition(i, j)];
                        numArr[numPosition(i, j)] = 0;
                    }
                }
            }
        } else if (action == "down") {
            for (var j = 0; j < 4; j++) {
                // 数字重合操作
                for (var i = 1; i < 4; i++) {
                    if (numArr[numPosition(i, j)] == numArr[numPosition(i - 1, j)]) {
                        numArr[numPosition(i, j)] += numArr[numPosition(i - 1, j)];
                        numArr[numPosition(i - 1, j)] = 0;
                    }
                }
                // 数字堆叠操作
                for (var i = 1; i < 4; i++) {
                    if (numArr[numPosition(i, j)] == 0) {
                        numArr[numPosition(i, j)] = numArr[numPosition(i - 1, j)];
                        numArr[numPosition(i - 1, j)] = 0;
                    }
                }
            }
        }
        numRandom();
        numInit();
    }
    // 随机函数，min<=返回值<=max
    function getRandom(min, max) {
        return min + Math.floor(Math.random() * (max - min + 1));
    }

    // 随机生成1~n 个min<=返回值<=max
    function getRandom2(n, min, max) {
        var arr = [];
        for (var i = 0; i < 3; i++) {
            var num = getRandom(min, max);
            if (!arr.includes(num)) {
                arr.push(num);
            }
        }
        if (arr.length == 0)
            arr.push(0);
        return arr;
    }
    // 随机生成新数字
    function numRandom() {
        randomArr = [];
        var empty = [];
        var newNum = [2, 2, 4];
        for (var i = 0; i < 16; i++) {
            if (numArr[i] == 0) {
                empty.push(i);
            }
        }
        // 获取新的数字的位置
        var arr = getRandom2(3, 0, empty.length - 1);
        for (var i = 0; i < arr.length; i++) {
            randomArr.push(empty[arr[i]]);
            numArr[empty[arr[i]]] = newNum[i];
        }
        numInit();
    }
    // 生成arr数组对应的html数值
    function numInit() {
        var item = $('.gameBox .row .item');
        var num = 0;
        for (var i = 0; i < 16; i++) {
            if (numArr[i] == 0) {
                num = '';
            } else {
                num = numArr[i];
            }
            item.eq(i).html(num);
            // 背景色上色
            switch (numArr[i]) {
                case 0:
                    item.eq(i).css('background', '');
                    break;
                case 2:
                    item.eq(i).css('background', 'rgb(250, 225, 188)');
                    break;
                case 4:
                    item.eq(i).css('background', 'rgb(202, 240, 240)');
                    break;
                case 8:
                    item.eq(i).css('background', 'rgb(117, 231, 193)');
                    break;
                case 16:
                    item.eq(i).css('background', 'rgb(240, 132, 132)');
                    break;
                case 32:
                    item.eq(i).css('background', 'rgb(181, 240, 181)');
                    break;
                case 64:
                    item.eq(i).css('background', 'rgb(182, 210, 246)');
                    break;
                case 128:
                    item.eq(i).css('background', 'rgb(255, 207, 126)');
                    break;
                case 256:
                    item.eq(i).css('background', 'rgb(250, 216, 216)');
                    break;
                case 512:
                    item.eq(i).css('background', 'rgb(124, 183, 231)');
                    break;
                case 1024:
                    item.eq(i).css('background', 'rgb(225, 219, 215)');
                    break;
                case 2048:
                    item.eq(i).css('background', 'rgb(221, 160, 221)');
                    break;
                case 4096:
                    item.eq(i).css('background', 'rgb(250, 139, 176)');
                    break;
            }
        }
        for (var i = 0; i < randomArr.length; i++) {
            item.eq(randomArr[i]).css('background', 'rgb(250, 139, 176)');
            item.eq(randomArr[i]).hide();
            item.eq(randomArr[i]).show("slow");
        }
        gameScore = scoreSum();
        $('#nowScore').html(gameScore);
        if (isNaN(maxScore)) {
            maxScore = 0;
            localStorage.maxScore = maxScore;
        }
        if (maxScore < gameScore) {
            maxScore = gameScore;
            localStorage.maxScore = maxScore;
        }
        // console.log(maxScore);
        $('#maxScore').html(maxScore);
    }

    function scoreSum() {
        var sum = 0;
        for (var i = 0; i < 16; i++) {
            sum += numArr[i] * 4;
        }
        return sum;
    }
    // -----------------------------------页面按钮监听事件----------------------------
    $('body').keydown(function(e) {
        switch (e.keyCode) {
            case 37:
                action = "left";
                // console.log(action);
                numMove();

                break;
            case 38:
                action = "up";
                numMove();
                // console.log(action);
                break;
            case 39:
                action = "right";
                numMove();
                // console.log(action);
                break;
            case 40:
                action = "down";
                numMove();
                // console.log(action);
                break;

        }

    });

})